package morning.cat.study.fst;

import morning.cat.study.commons.User;
import org.junit.Test;
import org.nustaq.serialization.FSTConfiguration;
import org.nustaq.serialization.FSTObjectInput;
import org.nustaq.serialization.FSTObjectOutput;

import java.io.*;
import java.util.Calendar;

/**
 * 参考：
 * https://github.com/RuedigerMoeller/fast-serialization/wiki/Serialization
 * https://www.oschina.net/p/fst
 */
public class SerializableTest {

    static FSTConfiguration fstConfiguration = FSTConfiguration.createDefaultConfiguration();

    @Test
    public void writeObject6() throws IOException {
        User user = new User();
        user.setId(1006);
        user.setName("晨猫");
        user.setDate(Calendar.getInstance().getTime());
        user.setStr("xxx");
        System.out.println(user);

        //FSTConfiguration fstConfiguration = FSTConfiguration.createDefaultConfiguration();

        OutputStream outputStream = new FileOutputStream(new File("fst.bin"));
        FSTObjectOutput fstObjectOutput = fstConfiguration.getObjectOutput(outputStream);
        fstObjectOutput.writeObject(user, User.class);
        // DON'T out.close() when using factory method;
        fstObjectOutput.flush();
        outputStream.close();
    }

    @Test
    public void readObject6() throws Exception {
        //FSTConfiguration fstConfiguration = FSTConfiguration.createDefaultConfiguration();

        InputStream ois = new FileInputStream("fst.bin");
        FSTObjectInput fstObjectInput = fstConfiguration.getObjectInput(ois);
        User user = (User) fstObjectInput.readObject(User.class);
        ois.close();
        System.out.println(user);
    }

}
